package net.pulseproject.router.modules.logic;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

abstract class AbstractValueModifier<T> implements ValueModifier<T> {

	private static final Log LOG = LogFactory.getLog(AbstractValueModifier.class);
	
	private final int valueSourcesCount;
	
	private final Map<T, Integer> lastSentValues = new HashMap<T, Integer>();
	
	public AbstractValueModifier(final int valueSourcesCount) {
		this.valueSourcesCount = valueSourcesCount;
	}

	@Override
	public final int calculateValue(final T source, final int passedValue) {
		LOG.debug("calculateValue(source, passedValue=" + passedValue + ")");
		
		this.lastSentValues.put(source, Integer.valueOf(passedValue));
		
		return this.calculateConcreteValue(this.lastSentValues.values());
	}
	
	abstract int calculateConcreteValue(final Collection<Integer> values);
	
	final int getValueSourcesCount() {
		return this.valueSourcesCount;
	}
}
